package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.common.ResultType;
import com.engineering.elink.domain.AuthUser;
import com.engineering.elink.service.OAuthApiService;
import com.engineering.elink.util.MapConvert;

@RestController
@RequestMapping("/api/oauth2")
@Api(value = "第三方登录授权接口")
public class OAuthController {
	@Resource(name="qqAuth")
	private OAuthApiService oAuthQQApiService;
	@Resource(name="weiXinOAuth")
	private OAuthApiService oAuthWeixinApiService;
	@Resource(name="sinaOAuth")
	private OAuthApiService oAuthSinaApiService;
	
	/**
	 * 获取第三方的登录链接
	 * @return
	 */
	@RequestMapping(value="/getUrl",method = RequestMethod.GET)
	@ApiOperation(value="获取第三方的登录链接",httpMethod="GET",notes="获取第三方的登录链接")
	public Map<String, Object>getUrl(){
		Map<String, Object> map = new HashMap<String, Object>();
		      map.put("QQ", oAuthQQApiService.authorize());
		      map.put("Weixin", oAuthWeixinApiService.authorize());
		      map.put("Sina", oAuthSinaApiService.authorize());
			return MapConvert.getMap(ResultType.SUCCESS,map);
	}
	
	/**
	 * 第三方登录授权回调函数
	 * QQ
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/authQQLoad",method = RequestMethod.GET)
	public Map<String, Object>AuthQQLoad(@RequestParam(value="code")String code,@RequestParam(value="state")String state){
		try {
			@SuppressWarnings("unused")
			AuthUser user = 	oAuthQQApiService.getUser(code);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//TODO 数据入库
		return MapConvert.getMap(ResultType.SUCCESS);
	}
	/**
	 * 第三方登录授权回调函数
	 * Weixin
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/authWeixinLoad",method = RequestMethod.GET)
	public Map<String, Object>AuthWeixinLoad(@RequestParam(value="code")String code,@RequestParam(value="state")String state){
		try {
			@SuppressWarnings("unused")
			AuthUser user = 	oAuthWeixinApiService.getUser(code);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//TODO 数据入库
		return MapConvert.getMap(ResultType.SUCCESS);
	}
	
	/**
	 * 第三方登录授权回调函数
	 * Sina
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/authSinaLoad",method = RequestMethod.GET)
	public Map<String, Object>AuthSinaLoad(@RequestParam(value="code")String code,@RequestParam(value="state")String state) {
		try {
			@SuppressWarnings("unused")
			AuthUser user = oAuthSinaApiService.getUser(code);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//TODO 数据入库
		return MapConvert.getMap(ResultType.SUCCESS);
	}
}
